package com.example.springboot009uploadfile.controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.Part;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/7/4 上午 10:38
 * @version: v1.0
 */
@Controller
public class UploadFileController {

    /**
     * @Description: 上传多个参数
     * @auther: uu
     * @date: 2023/7/4 下午 04:26
     */
    @PostMapping("/upload3")
    public String onload3(@RequestParam("upfile") MultipartFile[] multipartFiles){
        Map<String, Object> info = new HashMap<>();
        for (MultipartFile multipartFile : multipartFiles){
            if (!multipartFile.isEmpty()){
                info.put("上传文件类型参数", multipartFile.getName());
                info.put("内容类型", multipartFile.getContentType());
                var ext = "";
                var filename = multipartFile.getOriginalFilename();
                if (filename.indexOf(".")>0){
                    ext = filename.substring(filename.indexOf("."));
                }
                var newFileName = UUID.randomUUID().toString() + ext;
                var path = "C:\\Users\\uu\\Desktop\\springboot3\\" + newFileName;
                info.put("上传后文件名", newFileName);
                try {
                    multipartFile.transferTo(new File(path));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return "redirect:/";
    }
    /**
     * @Description: 单个文件上传
     * @auther: uu
     * @date: 2023/7/4 下午 03:43
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("upfile")MultipartFile multipartFile){

        Map<String, Object> info = new HashMap<>();
        if (!multipartFile.isEmpty()){
            info.put("上传文件类型参数",multipartFile.getName());
            info.put("内容类型",multipartFile.getContentType());
            var ext = "unknown";
            var filename = multipartFile.getOriginalFilename();
            if (filename.indexOf(".") > 0){
                ext = filename.substring(filename.indexOf(".")+1);
            }
            var newFileName = UUID.randomUUID()+"." + ext;
            var path = "C:\\Users\\uu\\Desktop\\springboot3\\"+newFileName;
            System.out.println(path);
            info.put("上传后的文件名称", newFileName);
            try {
                multipartFile.transferTo(new File(path));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            info.forEach((k,v)-> System.out.println(k+"->"+v));
        }
        return "redirect:/";
    }
    /**
     * @Description: 使用原生的方式
     * @auther: uu
     * @date: 2023/7/4 下午 03:21
     */
    @PostMapping("/upload2")
    public String upload2(HttpServletRequest request){
        try {
            for (Part part : request.getParts()){
                String fileName = extractFileName(part);
                System.out.println(fileName);
                if (fileName.isEmpty()){
                    return "redirect:/";
                }
                part.write(fileName);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        }
        return "redirect:/";
    }
    private String extractFileName(Part part){
        //form-data; name="dataFile"; filename="header.png"
        String contentDisp = part.getHeader("content-disposition");
        String[] items = contentDisp.split(";");
        for (String s : items){
            if (s.trim().startsWith("filename")){
                return s.substring(s.indexOf("=")+2, s.length()-1);
            }
        }
        return "";
    }


}
